package com.ruiyangwang.ListenBook.service;

import com.ruiyangwang.ListenBook.dataObject.TidToCountDo;
import com.ruiyangwang.ListenBook.dataObject.TrackDo;
import com.ruiyangwang.ListenBook.repository.RelationRepo;
import com.ruiyangwang.ListenBook.repository.TrackRepo;
import com.ruiyangwang.ListenBook.util.Log;
import com.ruiyangwang.ListenBook.viewObject.ListTrackView;
import com.ruiyangwang.ListenBook.viewObject.PaginationView;
import com.ruiyangwang.ListenBook.viewObject.StudioTrackListView;
import com.ruiyangwang.ListenBook.viewObject.UserV0;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author syzygy
 * @date 2022/07/12 17:28
 **/
public class TrackService {
    private final TrackRepo trackRepo = new TrackRepo();
    //???干啥的
    private final RelationRepo relationRepo =new RelationRepo();

    public StudioTrackListView listOfUser(UserV0 userV0,int page){
        StudioTrackListView res =new StudioTrackListView();
        res.currentUser =userV0;

        //1.规定每页共有5条
        int countParPage = 5;
        Log.println("每页个数" + countParPage);
        int count =trackRepo.selectCountByUid(userV0.uid);
        Log.println("属于当前的用户的音频共有：" + count);
        if(count == 0){
           res.pagination = new PaginationView();
           res.pagination.countPerPage = countParPage;
           res.pagination.currentPage = 1;
           res.pagination.totalPage = 0;
           res.trackList = new ArrayList<>();
           return res;
        }
        int totalPage = count / countParPage;
        if(count % countParPage > 0){
            totalPage++;
        }
        Log.println("共有" + totalPage + "页");
        if(page < 1){
            page = 1;
        }
        if(page > totalPage){
            page = totalPage;
        }
        int offset =(page - 1) *countParPage;
        //从数据库中查询
        List<TrackDo> list =trackRepo.selectListByUidLimitOffset(userV0.uid,countParPage,offset);
        Log.println("查询的结果为" + list);

        if(!list.isEmpty()) {
            Log.println("结果不为空，需要查询关联数量");

            List<Integer> tidList = new ArrayList<>();
            for (TrackDo trackDo : list) {
                tidList.add(trackDo.tid);
            }
            //2.拿着tidList从relations表中找到需要的结果存入map中
            Map<Integer, Integer> tidToRefCountMap = new HashMap<>();
            List<TidToCountDo> tidToCountDoList = relationRepo.selectCountByTidListGroupByTidList(tidList);
            Log.println("查询relations到的结果为 " + tidToCountDoList);
            for (TidToCountDo tidToCountDo : tidToCountDoList) {
                tidToRefCountMap.put(tidToCountDo.tid, tidToCountDo.refCount);
            }
            Log.println("tid to ref count map:" + tidToRefCountMap);

            //3.将trackDo ->ListTrackView
            List<ListTrackView> resultList = new ArrayList<>();
            for (TrackDo trackDo : list) {
                ListTrackView view = new ListTrackView(trackDo, tidToRefCountMap.getOrDefault(trackDo.tid, 0));
                resultList.add(view);
            }
            Log.println("转换为准备输出的结果" + resultList);
            res.trackList = resultList;
        }else{
            Log.println("结果为空");
            res.trackList = new ArrayList<>();
            }
        //查询页面的相关信息

        PaginationView paginationView = new PaginationView();
        paginationView.countPerPage =countParPage;
        paginationView.currentPage = page;
        paginationView.totalPage =totalPage;
        res.pagination = paginationView;

        return res;

        }
}